<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Border;

//下载excel用户导入模版
class UserTemplateExport implements FromCollection, WithStrictNullComparison, WithEvents
{
    private $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $title = ["手机号", "密码", "推荐人手机号", "优先级"];
        $data = $this->data;

        return collect([
            $title,
            $data
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getDelegate()->getColumnDimension('A')->setWidth(35);
                $event->sheet->getDelegate()->getColumnDimension('B')->setWidth(30);
                $event->sheet->getDelegate()->getColumnDimension('C')->setWidth(35);
                $event->sheet->getDelegate()->getColumnDimension('D')->setWidth(15);
                $event->sheet->getDelegate()->getStyle("A1:D1")->getFont()->setBold(true);
                $event->sheet->getDelegate()->getStyle("A1:D6")->getAlignment()->setVertical('center');
                $event->sheet->getDelegate()->getStyle("A1:D6")->getAlignment()->setHorizontal('center');
            }
        ];
    }

}
